package com.coocaa.ops.admin.v1.dict.biz;

import com.coocaa.ops.admin.v1.dict.mapper.IDictInfoMapper;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.dict.entity.DictInfoEntity;
import com.coocaa.ops.common.tool.date.DateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author bijiahao
 * @date : 2018/9/19.
 * @description
 */
@Service
public class DictInfoBizImpl extends BaseBizImpl<IDictInfoMapper, DictInfoEntity> implements IDictInfoBiz {

    @Override
    public List<DictInfoEntity> listUserBusinessType(Integer userId) {
        return getMapper().listDictByCategoryAndUserId("businessType", userId);
    }

    /**
     * 新增-数据字典信息
     *
     * @param entity
     * @return
     */
    @Override
    public DictInfoEntity add(DictInfoEntity entity) {
        Date date = DateUtil.getCurDate();
        entity.setCreatedDate(date);
        entity.setLastUpdateDate(date);
        getMapper().insert(entity);
        return entity;
    }

    @Override
    public DictInfoEntity queryByName(String dictName) {
        QueryTemplate wrapper = QueryTemplate.getEqQueryTemplate("dict_name", dictName);
        List<DictInfoEntity> list = getMapper().selectList(wrapper);
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }

    @Override
    public void updateDictStatus(Integer dictId, Integer status) {
        DictInfoEntity update = new DictInfoEntity();
        update.setId(dictId);
        update.setFlag(status);
        this.updateById(update);
    }

    /**
     * 获取默认主题图标的最大序号【icon_22】
     *
     * @return
     */
    @Override
    public Integer queryMaxIconIndex() {
        return getMapper().queryMaxIconIndex();
    }
}
